package com.star.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.star.goods.feign.SkuFeign;
import com.star.goods.pojo.Sku;
import com.star.goods.pojo.Spu;
import com.star.search.dao.EsManagerMapper;
import com.star.search.pojo.SkuInfo;
import com.star.search.service.EsManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/*
			  ________   ___   ___        ___   ________   ___   ___        ___
	 ________|\   __  \_|\  \_|\  \______|\  \_|\   __  \_|\  \_|\  \______|\  \_________
	 \       \ \  \_\ /_\ \  \\ \  \     \ \  \\ \  \_\ /_\ \  \\ \  \     \ \  \        \
	  \       \ \   __  \\ \  \\ \  \     \ \  \\ \   __  \\ \  \\ \  \     \ \  \        \
	   \       \ \  \_\  \\ \  \\ \  \____ \ \  \\ \  \_\  \\ \  \\ \  \____ \ \  \        \
	    \       \ \_______\\ \__\\ \_______\\ \__\\ \_______\\ \__\\ \_______\\ \__\        \
	     \       \|_______| \|__| \|_______| \|__| \|_______| \|__| \|_______| \|__|         \
	 	  \___________________________________________________________________________________\

*/
@Service
public class EsManagerServiceImpl implements EsManagerService {
    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;
    @Autowired
    private SkuFeign skuFeign;
    @Autowired
    private EsManagerMapper esManagerMapper;

    //插入索引和mapping
    @Override
    public void createIndexAndMapping() {
        //创建索引
        elasticsearchTemplate.createIndex(SkuInfo.class);
        //插入mapping
        elasticsearchTemplate.putMapping(SkuInfo.class);
    }
    //插入数据
    @Override
    public void importAll() {
        importDate("all");
    }

    //根据spuId插入sku数据
    @Override
    public void importSkuBySpuId(String spuId) {
        importDate(spuId);
    }

    @Override
    public void delDataSkuBySpuId(String spuId) {
        List<Sku> skuList = skuFeign.findListBySpuId(spuId);
        if (skuList == null || skuList.size() <= 0) {
            throw new RuntimeException("商品数据未查询到，无法创建索引");
        }
        for (Sku sku : skuList) {
            esManagerMapper.deleteById(Long.parseLong(sku.getId()));
        }
    }

    /**
     * 提取公共导入方法
     * @param spuId
     */
    public void importDate(String spuId) {
        List<Sku> skuList = skuFeign.findListBySpuId(spuId);
        if (skuList == null || skuList.size() <= 0) {
            throw new RuntimeException("商品数据不存在");
        }
        //将数据转化为JSON
        String spuJSON = JSON.toJSONString(skuList);
        //将数据存放到skuInfo中
        List<SkuInfo> skuInfos = JSON.parseArray(spuJSON, SkuInfo.class);
        //遍历数据将spec数据放入specMap
        for (SkuInfo skuInfo : skuInfos) {
            String spec = skuInfo.getSpec();
            Map specMap = JSON.parseObject(spec, Map.class);
            skuInfo.setSpecMap(specMap);
        }
        //保存数据
        esManagerMapper.saveAll(skuInfos);
    }
}
